package com.deng.api.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deng.api.dto.UserDTO;
import com.deng.api.entity.SysUser;
import com.deng.api.entity.SysUserRole;
import com.deng.api.log.Log;
import com.deng.api.service.ISysUserRoleService;
import com.deng.api.service.ISysUserService;
import com.deng.api.vo.UserPassVO;
import com.deng.commons.common.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author deng
 * @since 2022-08-10
 */
@RestController
@Slf4j
@RequestMapping("/api/users")
@Api(tags = "用户相关接口")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @ApiOperation("修改密码")
    @PostMapping("/modifyPwd")
    @ApiImplicitParams({@ApiImplicitParam(name = "oldPassword"),@ApiImplicitParam(name = "newPassword")})
    public BaseResult modifyPwd(@RequestBody UserPassVO userPassVO){
        return iSysUserService.modifyPassword(userPassVO);
    }

    @PostMapping("/uploadAvatar")
    @ApiOperation("上传头像")
    @ApiImplicitParam(name = "文件类型")
    public BaseResult uploadAvatar(MultipartFile avatar){
        return iSysUserService.uploadAvatar(avatar);
    }

    @GetMapping
    @ApiOperation("获取用户列表")
    @Log("获取用户列表")
    @ApiImplicitParam(name = "传递用户参数信息")
    public BaseResult getUsers(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
            UserDTO userDTO){
        return iSysUserService.getUsers(page,size,userDTO);
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "删除用户")
    public BaseResult delete(@RequestBody Set<Long> ids){
        return iSysUserService.delete(ids);
    }

    @PostMapping("/add")
    @ApiOperation("添加新用户")
    public BaseResult addUser(@RequestBody SysUser sysUser){
        return iSysUserService.addUser(sysUser);
    }

    /**
     * 用户信息修改
     * 删除用户角色id
     * @param sysUser
     * @return
     */
    @PostMapping("/edit")
    @Log("修改用户信息")
    @ApiOperation("修改用户信息")
    public BaseResult editUser(@RequestBody SysUser sysUser){
        return iSysUserService.editUser(sysUser);
    }

    @GetMapping("/download")
    @ApiOperation("用户文件导出")
    public void download(HttpServletResponse response){
        iSysUserService.download(response);
    }

    @Log("获取分配人事专员列表")
    @ApiOperation("获取分配人事专员列表")
    @GetMapping("/listConsultant/{companyId}")
    public BaseResult listConsultant(@PathVariable Long companyId){
        return iSysUserService.listConsultant(companyId);
    }
}
